package grape.file.rest.tool;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * pdf操作工具类
 * Created by yangwei
 * Created at 2018/10/30 15:38
 */
public class PdfTool {

    public static List<BufferedImage> pdf2Image(PDDocument pdDocument,String formatName, int dpi) throws IOException {
        List<BufferedImage> result = new ArrayList<>();
        if (pdDocument != null) {
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            /* dpi越大转换后越清晰，相对转换速度越慢 */
            int pages = pdDocument .getNumberOfPages();
            for (int i = 0; i < pages; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                    result.add(image);
            }
            try {
                pdDocument.close();
            } catch (IOException e) {
            }
        }
        return result;
    }
}
